
# python3 不可以直接这样用的:

import base64

# 需要这样

str1 = "this is string example....wow!!!"

str3 = str1.encode(encoding ='utf-8',errors = 'strict');
print (str3),
print ('')

# str3 转成bytes 的string
# str3 = str1.encode(encoding ='utf-8',errors = 'strict');
# print (str3),
# print ('')

# bytes 再进行 base64 编码
str4= base64.b64encode(str3)

print (str4)
print ('')

# 再base64 decode 一下
# print (str4.decode())
# print ('')

# base64 解码
enstr = base64.b64decode(str4.decode())
print(enstr.decode())

def str_to_base64(string):
    
    str3 = string.encode(encoding ='utf-8',errors = 'strict');
    str4= base64.b64encode(str3)
    enstr = base64.b64decode(str4.decode())
    return enstr


image_path = r"D:\PycharmProjects\mirrors-odoo-14.0\odoo\Images\美女.png"
with open(image_path, 'rb') as f:
	image = f.read()
	image_base64 = str(base64.b64encode(image), encoding='utf-8')

def img_to_base64(image):
    image_base64 = str(base64.b64encode(image), encoding='utf-8')
    return image_base64

def img_path_to_base64(image_path):
    with open(image_path, 'rb') as f:
        image = f.read()
        image_base64 = str(base64.b64encode(image), encoding='utf-8')
    return image_base64
    